﻿using System;

namespace DoubanFM.Model
{
    public enum MusicSearchType
    {
        New,
        Skip,
        Like,
        Unlike,
        Garbage,
        End,
        More
    }

    public class MusicSearch
    {
        private const string Url = "http://douban.fm/j/mine/playlist?type={0}&sid={1}&channel={2}&pt={3}&from={4}&r={5}";

        public string SearchUrl { get; set; }

        public MusicSearch(MusicSearchType type, string sid, string channel)
        {
            SearchUrl = String.Format(Url, GetType(type), sid, channel, GetPt(), GetFrom(), GetRandom());
        }

        private static string GetType(MusicSearchType type)
        {
            switch (type)
            {
                case MusicSearchType.New:
                    return "n";
                case MusicSearchType.Skip:
                    return "s";
                case MusicSearchType.Like:
                    return "r";
                case MusicSearchType.Unlike:
                    return "u";
                case MusicSearchType.Garbage:
                    return "b";
                case MusicSearchType.End:
                    return "e";
                case MusicSearchType.More:
                    return "p";
                default:
                    throw new ArgumentOutOfRangeException("type");
            }
        }

        private static string GetPt()
        {
            // TODO: I don't really know what pt parameter is, just send a random number for know
            var random = new Random();
            var a = random.Next(0, 255);
            var b = random.Next(0, 9);
            return a + "." + b;
        }

        private static string GetFrom()
        {
            return "metroclient";
        }

        private static string GetRandom()
        {
            var random = new Random();
            var bytes = new byte[8];
            random.NextBytes(bytes);
            return (BitConverter.ToUInt64(bytes, 0) % 0xFFFFFFFFFF).ToString("x10");
        }
    }
}
